//
//  String+Extension.swift
//  mmda
//
//  Created by 李陆涛 on 2018/4/18.
//  Copyright © 2018年 mmda. All rights reserved.
//

import UIKit

extension String {
    
    
    func getQiniuThumb(_ height:Int) -> String {
        
        if self.contains("?") {
            return self
        }
        
        return "\(self)?imageView2/2/h/\(height)"
    }
    
    func getWidth(font:UIFont) -> CGFloat {
        
        return size(font: font).width
    }
    
    func getHeight(font:UIFont,width:CGFloat) -> CGFloat {
        
        return size(font: font, width: width).height
    }
    
    func size(font:UIFont,width:CGFloat = CGFloat(MAXFLOAT), height:CGFloat = CGFloat(MAXFLOAT)) -> CGSize {
        
        let str = NSString(string: self)
        let rect = str.boundingRect(with: CGSize(width: width, height: height), options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: [NSAttributedStringKey.font:font], context: nil)
        
        return rect.size
    }
    
    func subString(from:Int, to:Int) -> String {
        
        let index1 = self.index(self.startIndex, offsetBy: from)
        let index2 = self.index(self.startIndex, offsetBy: to)
        
        return String(self[index1..<index2])
    }
    
    func subString(from:Int, length:Int) -> String {
        
        let index1 = self.index(self.startIndex, offsetBy: from)
        let index2 = self.index(index1, offsetBy: length)
        
        return String(self[index1..<index2])
    }
    
    
}
